/*
 */

package com.railway.notice.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.org.rapid_framework.page.Page;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.railway.common.base.BaseStruts2Action;
import com.railway.common.constants.SysConstant;
import com.railway.common.util.Json;
import com.railway.common.util.StaticMap;
import com.railway.notice.model.Notice;
import com.railway.notice.service.INoticeManager;
import com.railway.notice.vo.NoticeQuery;
import com.railway.common.util.StaticMethod;

/**
 * @author  jiangcheng
 * @version 1.0
 * @since 1.0
 */

public class NoticeAction extends BaseStruts2Action implements Preparable,
		ModelDriven {
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;

	//forward paths
	protected static final String QUERY_JSP = "/admin/notice/query.jsp";
	protected static final String LIST_JSP = "/admin/notice/list.jsp";

	protected static final String CREATE_JSP = "/admin/notice/create.jsp";

	protected static final String EDIT_JSP = "/admin/notice/edit.jsp";
	protected static final String SHOW_JSP = "/admin/notice/show.jsp";

	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/notice/Notice/list.do";

	private INoticeManager noticeManager;

	private Notice notice;
	java.lang.String id = null;
	private String[] items;
	private String param;

	public void setParam(String param) {
		this.param = param;
	}

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			notice = new Notice();
		} else {
			notice = (Notice) noticeManager.getById(id);
		}
	}

	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setNoticeManager(INoticeManager manager) {
		this.noticeManager = manager;
	}

	public Object getModel() {
		return notice;
	}

	public void setId(java.lang.String val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/** 执行搜索 */
	public String list() {
		return LIST_JSP;
	}

	public String listAjax() {
		
		NoticeQuery query = newQuery(NoticeQuery.class, DEFAULT_SORT_COLUMNS);
		
		Page page = null;
		Map map = new HashMap();
		try{
		page= noticeManager.findPage(query);
		
	
		map.put("total", page.getTotalCount());
		map.put("rows", page.getResult());
		List list = page.getResult();
		if( list != null) {
			for(int i = 0 ; i < list.size(); i++){
				
				Notice nc = (Notice)list.get(i); 
				nc.setCreator(nc.getCreator() == null?"":StaticMap.getUserValue(nc.getCreator()));
			}
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return writeAjaxResponse(Json.toJson(map));
		
	}

	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	/** 查看对象*/
	public String showAjax() {
		  
		  return writeAjaxResponse(Json.toJson(notice)); 
	}
	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}

	/** 保存新增对象 */
	public String save() {
		String current_user_id = (String) this.getRequest().getSession().getAttribute(SysConstant.USER_ID);
		notice.setCreatetime(new Date(System.currentTimeMillis()));
		String expire_time = notice.getExpiretimestring();
	    notice.setExpiretime(StaticMethod.dateString2Date(expire_time));
		//放入创建者id
        notice.setCreator(current_user_id);
		noticeManager.save(notice);
		return LIST_ACTION;
	}

	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}

	/**保存更新对象*/
	public String update() {
		String expire_time = notice.getExpiretimestring(); 
		notice.setExpiretime(StaticMethod.dateString2Date(expire_time));
		noticeManager.update(this.notice);
		return LIST_ACTION;
	}

	/**删除对象*/
	public String delete() {
		if (items != null)
			for (String ids : items) {
				String[] idArray = ids.split(",");
				for (String id : idArray)
					noticeManager.removeById(id);
			}
		return LIST_ACTION;
	}

}
